<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <title>个人资料</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <style>
        .layui-fluid {
            padding-top: 20px;
            padding-bottom: 20px;
            background: #fff;
        }
    </style>
</head>
<body>
<div class="layui-fluid">
    <form class="layui-form" method="post" enctype="multipart/form-data" lay-filter="form">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" value="" placeholder="请输入用户名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="text" name="password" value="" placeholder="请输入密码，留空则不修改" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="*">提交</button>
            </div>
        </div>
    </form>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.use(['jquery', 'layer', 'form', 'element'], function(){
        let form = layui.form
            ,$ = layui.jquery
            ,layer = layui.layer
            ,result = <?=$result ?? '{}' ?>;

        form.val('form', result);

        form.on('submit(*)', function (data) {
            let elem = $(data.elem);

            elem.addClass('layui-btn-disabled');

            fetch(data.form.action, {
                body: new FormData(data.form)
                ,method: 'POST'
                ,credentials: 'same-origin'
                ,headers: {
                    'X-Requested-With': 'XMLHttpRequest'
                }
            })
                .then(response => response.json())
                .then(function (result) {
                    elem.removeClass('layui-btn-disabled');

                    if (0 == result.code) {
                        layer.msg(result.msg, {end: () => location.reload()});
                    } else {
                        layer.alert(result.msg);
                    }
                })
                .catch(function (error) {
                    elem.removeClass('layui-btn-disabled');
                    layer.alert('发生错误:' + error);
                });

            return false;
        });
    });
</script>
</body>
</html>